package com.kye.express.business.complete_information;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.SharedPreferencesConfig;
import com.kye.model.SharedPreferencesManager;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.user.User;
import com.kye.model.bean.user.request.RequestCompleteInformation;
import com.kye.model.bean.user.request.RequestGetUser;
import com.kye.model.service.UserService;

import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public interface CompleteInformationContract {
    interface CompleteInformationView extends IView, LoadingView, ErrorView {

        void onSuccess();
    }

    class CompleteInformationPresenter extends BasePresenter<CompleteInformationView> {

        public void completeInformation(final User user, String name, String company) {
            RetrofitManager.getInstance()
                    .createOpenApiService(UserService.class)
                    .completeInformation(new RequestCompleteInformation(user.getPhone(), company, name), user.getToken())
                    .flatMap(new Function<OpenApiResult<Object>, SingleSource<OpenApiResult<User>>>() {
                        @Override
                        public SingleSource<OpenApiResult<User>> apply(OpenApiResult<Object> objectOpenApiResult) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createService(UserService.class)
                                    .getUserWithToken(new RequestGetUser(user.getPhone()), user.getToken());
                        }
                    })
                    .doOnSuccess(new Consumer<OpenApiResult<User>>() {
                        @Override
                        public void accept(OpenApiResult<User> userOpenApiResult) throws Exception {
                            SharedPreferencesManager.commit(SharedPreferencesConfig.KEY_NAME_CERTIFICATION, System.currentTimeMillis());

                            User newUser = userOpenApiResult.getData();
                            if (newUser == null) return;
                            newUser.setToken(user.getToken());
                            newUser.setRefreshToken(user.getRefreshToken());

                            UserSession.getInstance().setUser(newUser);
                        }
                    })
                    .compose(getView().<OpenApiResult<User>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<User>() {
                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<User> result, User user) {
                            getView().onSuccess();
                        }

                        @Override
                        public void onApiError(OpenApiResult<User> result, int code, String msg) {
                            getView().onError(msg);
                        }
                    });
        }
    }
}
